/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "downloadmanager.h"

#include <QTextStream>

#include <cstdio>

using namespace std;

DownloadManager::DownloadManager(QString path,QObject *parent)
    : QObject(parent)
{
    if(!path.isEmpty())
    {
        rootPath = path;
    }
}

DownloadManager::~DownloadManager()
{
    printf("释放下载器\n");
}

void DownloadManager::append(const QList<QJsonObject> &urls)
{
    for (const QJsonObject &obj : urls)
        append(obj);
    
    if (downloadQueue.isEmpty())
        QTimer::singleShot(0, this, &DownloadManager::finished);
}

void DownloadManager::append(const QJsonObject &json)
{
    if (downloadQueue.isEmpty())
        QTimer::singleShot(0, this, &DownloadManager::startNextDownload);
    
    downloadQueue.enqueue(json);
    ++totalCount;
}

bool DownloadManager::saveFileName(const QString &url)
{
    QString runPath = url;
    
    QDir dir;
    //qDebug()<<"创建路径 "<<runPath;
    if(dir.mkpath(runPath))
    {
        return true;
    }
    return false;
}

void DownloadManager::startNextDownload()
{
    if (downloadQueue.isEmpty()) {
        printf("%d/%d files downloaded successfully\n", downloadedCount, totalCount);
        emit finished();
        return;
    }
    
    QJsonObject obj = downloadQueue.dequeue();
    QString path = obj["path"].toString();
    QString ustr = obj["url"].toString();
    QString FileName;
    QUrl url(ustr);
    if(path.isEmpty())
    {
        FileName = rootPath + "/"+url.fileName();
    }else{
        FileName = rootPath + "/"+path;
    }
    qDebug()<<"download: "<<ustr;
    
    
    if(!saveFileName(QUrl(FileName).path().replace(url.fileName(),"")))
    {
        qDebug()<<"路径创建失败 ";
    }
    
    currentJson = obj;
    output.setFileName(FileName);
    currentFileName = FileName;
    
    if (!output.open(QIODevice::WriteOnly)) {
        qDebug()<<"文件打开失败: "<<output.errorString()<<" "<<FileName;
        startNextDownload();
        return;                 // skip this download
    }
    
    QNetworkRequest request(url);
    currentDownload = manager.get(request);
    //connect(currentDownload, &QNetworkReply::downloadProgress,this, &DownloadManager::downloadProgress);
    
    connect(currentDownload, &QNetworkReply::finished,
            this, &DownloadManager::downloadFinished);
    
    connect(currentDownload, &QNetworkReply::readyRead,
            this, &DownloadManager::downloadReadyRead);
    
    downloadTimer.start();
}

void DownloadManager::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    //progressBar.setStatus(bytesReceived, bytesTotal);
    
    // calculate the download speed
    double speed = bytesReceived * 1000.0 / downloadTimer.elapsed();
    QString unit;
    if (speed < 1024) {
        unit = "bytes/sec";
    } else if (speed < 1024*1024) {
        speed /= 1024;
        unit = "kB/s";
    } else {
        speed /= 1024*1024;
        unit = "MB/s";
    }
    
    //progressBar.setMessage(QString::fromLatin1("%1 %2")
    //.arg(speed, 3, 'f', 1).arg(unit));
    qDebug()<<QString::fromLatin1("%1 %2")
              .arg(speed, 3, 'f', 1).arg(unit);
    //progressBar.update();
}

void DownloadManager::downloadFinished()
{
    output.close();
    QFile file(currentFileName);
    file.open(QIODevice::ReadOnly);
    if (currentDownload->error()) {
        
        fprintf(stderr, "Failed: %s\n", qPrintable(currentDownload->errorString()));
        output.remove();
    } else {
        
        if (isHttpRedirect()) {
            reportRedirect();
            output.remove();
        } else {
            printf("Succeeded.\n");
            ++downloadedCount;
        }
    }
    
    currentDownload->deleteLater();
    QString sha1 = currentJson["sha1"].toString();
    if(!sha1.isEmpty()) //有无hash字段
    {
        QByteArray buffer = file.readAll();
        //校验是否完整
        if(!checkFileHash(buffer,sha1))
        {
            output.remove(); 
        }
    }
    
    emit downloadFinsh(currentFileName);
    file.close();
    startNextDownload();
}

void DownloadManager::downloadReadyRead()
{
    output.write(currentDownload->readAll());
}

bool DownloadManager::checkFileHash(QByteArray& buffer, QString sha1)
{
    QByteArray result = QCryptographicHash::hash(buffer,QCryptographicHash::Sha1);
    if(result.toHex() == sha1)
    {
        return true;
    }
    return false;
}

bool DownloadManager::isHttpRedirect() const
{
    int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    return statusCode == 301 || statusCode == 302 || statusCode == 303
            || statusCode == 305 || statusCode == 307 || statusCode == 308;
}

void DownloadManager::reportRedirect()
{
    int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    QUrl requestUrl = currentDownload->request().url();
    QTextStream(stderr) << "Request: " << requestUrl.toDisplayString()
                        << " was redirected with code: " << statusCode
                        << '\n';
    
    QVariant target = currentDownload->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (!target.isValid())
        return;
    QUrl redirectUrl = target.toUrl();
    if (redirectUrl.isRelative())
        redirectUrl = requestUrl.resolved(redirectUrl);
    QTextStream(stderr) << "Redirected to: " << redirectUrl.toDisplayString()
                        << '\n';
}
